$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/appinfo/list',
        datatype: "json",
        colModel: [			
			{ label: 'ID', name: 'id', index: "id", width: 45, key: true },
			{ label: '应用名称', name: 'appname', width: 75 },
            { label: '回调地址', name: 'callbackurl', sortable: false, width: 100 },
			{ label: '应用类别', name: 'appclassName', width: 90 },
			{ label: '应用系统根目录', name: 'approoturl', width: 100 },
			{ label: '状态', name: 'appstatus', width: 60, formatter: function(value, options, row){
				return value === 0 ? 
					'<span class="label label-danger">禁用</span>' : 
					'<span class="label label-success">正常</span>';
			}},
			{ label: '排序号', name: 'ordernum', index: "ordernum", width: 45},
			{label: '授权', index: '', width: 80, formatter: function (cellValue, options, rowObject) {
                    return '<a onclick="vm.authorization(\'' + rowObject.id + '\')"><i  class="glyphicon glyphicon-list glyphicon-lg" /></a>'
             }
            }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
    el:'#rrapp',
    data:{
    	q:{
            appname: null
        },
        showList: true,
        title:null,
        roleList:{},
        roleIds:{},
        appinfo:{
            
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
        	layer.open({
                type: 2,
                offset: '20px',
                skin: 'layui-layer-molv',
                title: "添加应用",
                area: ['920px', '550px'],
                shade: 0.3,
                shadeClose: false,
                content: "appinfoAdd.html"
                
            });
        },
        authorization: function(appid){
            //加载角色树
            $.get(baseURL + "sys/appinfo/appRoleList/"+appid, function(r){
                vm.roleList = r.roleMap.roleList;
                vm.roleIds = r.roleMap.roleIds;
                layer.open({
                    type: 1,
                    offset: '50px',
                    skin: 'layui-layer-molv',
                    title: "选择角色",
                    area: ['660px', '450px'],
                    shade: 0.3,
                    shadeClose: false,
                    content: jQuery("#roleLayer"),
                    btn: ['确定', '取消'],
                    btn1: function (index) {
                    	const appRight = {};
                        appRight.roleIds = vm.roleIds;
                        appRight.appid = appid;
                        $.ajax({
                            type: "POST",
                            url:  baseURL + "sys/appinfo/saveAppRight",
                            contentType: "application/json",
                            data: JSON.stringify(appRight),
                            success: function(r){
                                if(r.code === 0){
                                    alert('操作成功', function(){
                                        layer.close(index);
                                    });
                                }else{
                                    alert(r.msg);
                                }
                            }
                        });

                    }
                });
            })
        },
        update: function () {
        	var id = getSelectedRow();
            console.log(id);
            if(id == null){
                return ;
            }
            var appid=id.id;
            layer.open({
                type: 2,
                offset: '20px',
                skin: 'layui-layer-molv',
                title: "修改应用",
                area: ['920px', '550px'],
                shade: 0.3,
                shadeClose: false,
                content: "appinfoAdd.html?id="+appid
                
            });
        },
        getAppinfo: function(appinfoId){
            $.get(baseURL + "sys/appinfo/info/"+appinfoId, function(r){
                vm.appinfo = r.appinfo;
                vm.getAppclass();
            });
        },
        
        del: function () {
            var appinfoguids = getSelectedRows();
            if(appinfoguids == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/appinfo/delete",
                    contentType: "application/json",
                    data: JSON.stringify(appinfoguids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'appname': vm.q.appname},
                page:page
            }).trigger("reloadGrid");
        }
    }
});